package com.acwebsitedesign.uRateDrugs.web;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.acwebsitedesign.uRateDrugs.domain.Account;
import com.acwebsitedesign.uRateDrugs.domain.AccountService;
import com.acwebsitedesign.uRateDrugs.domain.Drug;
import com.acwebsitedesign.uRateDrugs.domain.DrugService;
import com.acwebsitedesign.uRateDrugs.domain.Rating;
import com.acwebsitedesign.uRateDrugs.domain.Review;
import com.acwebsitedesign.uRateDrugs.domain.ReviewService;

@Controller
@SessionAttributes({"newReview", "drugList", "aliasList", "drug"})

public class DrugController {
	
	@Autowired
	private DrugService drugService;
	@Autowired
	private ReviewService reviewService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private Validator validator;
	 
	@RequestMapping("/searchByDrugName.html")
	public ModelAndView searchByDrugName() {
		ModelAndView mav = new ModelAndView("searchByDrugName");
		return mav;
	}
	
	
	@RequestMapping("/searchResultsByDrugName.html")
	public ModelAndView searchByResultsDrugName(@RequestParam String name) {		
		List<Drug> drugList = drugService.getDrugByName(name);
		List<Drug> aliasList = drugService.getDrugByAlias(name);
		ModelAndView mav = new ModelAndView("searchResultsByDrugName");
		if (drugList.isEmpty() && aliasList.isEmpty()){
					return new ModelAndView("drugNotFound");	
				}

		mav.addObject(drugList);
		mav.addObject(aliasList);
		return mav;
	}
	
	@RequestMapping("/searchResultsDrugContains.html")
	public ModelAndView searchResultsDrugContains(@RequestParam String name) {		
		List<Drug> drugList = drugService.getDrugLike(name);
		ModelAndView mav = new ModelAndView("searchResultsByDrugName");
		if (drugList.isEmpty()){
					return new ModelAndView("drugNotFound");	
				}
		mav.addObject(drugList);
		return mav;
	}
	
	@RequestMapping("/drugDetail.html")
	public ModelAndView drugDetail(@RequestParam int id){
		
		List<Drug> drugList = drugService.getDrugById(id);
		Drug drug = drugList.get(0);
		List <Review> reviewList = reviewService.getReviewsForADrug(drug.getId());
		int number = reviewService.getRating(drug.getId());
		Rating rating = new Rating(number);
		ModelAndView mav = new ModelAndView("drugDetail");
		if (reviewList.size()>0){
			Review review = reviewList.get(0);
			mav.addObject(review);
		}
		mav.addObject(drug);
		mav.addObject(rating);
		return mav;
	}

	
	@RequestMapping("/reviewBegin.html")
	public ModelAndView reviewBegin(@ModelAttribute("drug") Drug drug) {
		
		Account account = new Account();
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		if (username.contentEquals("anonymousUser")){
			account = accountService.getUserByUsername("anonymous");	
		} else {
			account = accountService.getUserByUsername(username);	
		}
		
		Review review = new Review();
		ModelAndView mav = new ModelAndView("reviewBegin");
		mav.addObject("newReview", review);
		mav.addObject("account", account);
		mav.addObject("drug", drug);
		return mav;
	}
	
	@RequestMapping("/reviewSaved.html")
	public ModelAndView reviewSaved(@RequestParam String submit, @ModelAttribute("newReview") Review newReview,
			 BindingResult result) throws SQLException {
		
		validator.validate(newReview, result);
		if (result.hasErrors()){
		return new ModelAndView("reviewBegin"); 
		}
		reviewService.saveReview(newReview);
		ModelAndView mav = new ModelAndView("reviewSaved");
		return mav;

	}
	
	@RequestMapping("/allDrugReviews.html")
	public ModelAndView allDrugReviews(@ModelAttribute("drug") Drug drug){
		ModelAndView mav = new ModelAndView("allDrugReviews");
		List<Review> reviewList = reviewService.getReviewsForADrug(drug.getId());
		mav.addObject(drug);
		mav.addObject(reviewList);
		return mav;
	}
	

}